import { Select } from "antd";
import React, { useState, useCallback, useEffect, useMemo } from "react";
import { cloneDeep } from "lodash";
/**
 *
 * @param {*} skuList  第一个表格数据
 * @param {*} skuSizeList  系统商品sku， 尺码去重
 * @param {*} sizeSelectTableData  第二个表格数据
 * @param {*} setSizeSelectTableData
 * @returns
 */
export const useSizeList = ({
	skuList,
	skuSizeList,
	sizeSelectTableData,
	setSizeSelectTableData,
	matchedStoreSkuAllList,
	setMatchedStoreSkuAllList
}) => {
	const [storeIds, setStoreIds] = useState([]); // 供应商去重 {storeId , storeName , goodsCommonId}
	const [storeSizeColumns, setStoreSizeColumns] = useState([]); // 第二个表格，列配置

	const defaultCol = {
		name: "尺码",
		code: "sysSpecs",
		width: 200
	};
	// 变更数据。
	const handleChange = useCallback(
		(v, k, r, i) => {
			const d = sizeSelectTableData.map((item, index) => {
				if (item.sysSpecs === r.sysSpecs) {
					item[k] = v;
				}
				return item;
			});
			setSizeSelectTableData(d);
		},
		[sizeSelectTableData]
	);

	useEffect(() => {
		const arr = Array.isArray(storeIds)
			? storeIds.map(e => {
					return {
						name: e.storeName + `（商品编码${e.goodsCommonId}）`,
						code: e.storeId,
						width: 200,
						render: (v, record, index) => {
							const options = record[e.storeId + "Options"];

							return Array.isArray(options) ? (
								<Select
									allowClear
									className="width-full"
									value={v}
									onChange={value => {
										handleChange(value, e.storeId, record, index);
									}}
									options={options.map(e => ({ value: e, label: e }))}
								/>
							) : (
								<></>
							);
						}
					};
				})
			: [];
		setStoreSizeColumns([defaultCol, ...arr]);
	}, [storeIds]);

	useEffect(() => {
		const filterList = [];
		const storeIdAndNameList = [];
		skuList
			.filter(e => e.merge && e.found)
			.forEach(({ items }) => {
				items.forEach(item => {
					if (item.isSelected) {
						filterList.push(item);

						if (!storeIdAndNameList.find(e => e.storeId == item.storeId)) {
							storeIdAndNameList.push({ storeId: item.storeId, storeName: item.storeName, goodsCommonId: item.goodsCommonId });
						}
					}
				});
			});

		// 已匹配数据
		matchedStoreSkuAllList.forEach(item => {
			if (!storeIdAndNameList.find(e => e.storeId == item.storeId)) {
				storeIdAndNameList.push({ storeId: item.storeId, storeName: item.storeName, goodsCommonId: item.goodsCommonId });
			}
		});

		setStoreIds(storeIdAndNameList);

		const sizeObj = {}; // {storeId: [size, size, size]}
		const sizeSelectOptions = {};

		filterList.forEach(e => {
			const { size, storeId } = e;
			if (Array.isArray(size)) {
				if (sizeSelectOptions[storeId + "Options"]) {
					const arr = sizeSelectOptions[storeId + "Options"].concat(size);
					sizeObj[storeId] = "";
					sizeSelectOptions[storeId + "Options"] = Array.from(new Set(arr));
				} else {
					sizeObj[storeId] = "";
					sizeSelectOptions[storeId + "Options"] = size;
				}
			}
		});

		// 已匹配数据
		const matchedStoreSkuToSizeObj = {};
		matchedStoreSkuAllList.forEach(e => {
			const { size, storeId, goodsId } = e;

			matchedStoreSkuToSizeObj[goodsId] = size;

			if (sizeSelectOptions[storeId + "Options"]) {
				const arr = sizeSelectOptions[storeId + "Options"].concat([size]);
				sizeObj[storeId] = "";
				sizeSelectOptions[storeId + "Options"] = Array.from(new Set(arr));
			} else {
				sizeObj[storeId] = "";
				sizeSelectOptions[storeId + "Options"] = [size];
			}
		});
		const d = skuSizeList.map(({ specs, sizeMatchedSkuList }) => {
			// 已匹配数据
			if (Array.isArray(sizeMatchedSkuList) && sizeMatchedSkuList.length) {
				sizeMatchedSkuList.forEach(({ supplierId, supplierName, supplierSkuId, supplierSpuCode }) => {
					// 查询匹配的size
					const matchedSize = matchedStoreSkuToSizeObj[String(supplierSkuId)];
					// 写入
					sizeObj[String(supplierId)] = matchedSize;
				});
			}
			return {
				sysSpecs: specs,
				...sizeObj,
				...sizeSelectOptions
			};
		});
		setSizeSelectTableData(d);
	}, [skuList, setStoreIds, setSizeSelectTableData]);

	const tableKey = "SystemGoodsMatchIntelligentMatchTableSize" + storeIds.length;
	const searchSimpleTableProps = {
		tableKey,
		rowKey: "sysSpecs",
		// ///
		dataSource: sizeSelectTableData,
		columns: storeSizeColumns,
		footer: false
	};
	return {
		tableKey,
		searchSimpleTableProps
	};
};
